#include "TaskSPI.h"
#include "AR1020.h"

xQueueHandle QueueSPIReceive, QueueSPITransmit;
extern xSemaphoreHandle SyncSema;

static void spi_task(void *params)
{
	AR1020_Packet temp;

	while(1)
	{
		if(xQueueReceive(QueueSPIReceive, &temp, portMAX_DELAY) == pdTRUE)
		{
			if(temp.buffer.buffer[0] == 0x55)
			{
				SendPacket((uint8_t *)&temp.buffer.buffer);
				xSemaphoreTake(SyncSema, portMAX_DELAY);
				xQueueSend(QueueSPITransmit, &temp, portMAX_DELAY);
			}
		}
		else
		{
			vTaskDelay(100);
		}

	}
}

void InitTaskSPI(void)
{
	vSemaphoreCreateBinary(SyncSema);
	xSemaphoreTake(SyncSema, portMAX_DELAY);

	QueueSPIReceive = xQueueCreate( 5, sizeof(AR1020_Packet *));
	QueueSPITransmit = xQueueCreate( 5, sizeof(AR1020_Packet *));

	init_SPI1();

	xTaskCreate( spi_task, ( signed char * ) "SPI Task", 100, NULL, 1, NULL );
}
